package com.ruoyi.project.api.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.api.domain.WarrantyRecode;
import com.ruoyi.project.vehicle.domain.BinsMaintenanceInfo;
import com.ruoyi.project.vehicle.domain.BinsVehicleInfo;
import com.ruoyi.project.vehicle.service.IBinsMaintenanceInfoService;
import com.ruoyi.project.vehicle.service.IBinsVehicleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @title : ScanController
 * @Author : Administrator
 * @Date : 2025/4/10
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/api/vehicle")
public class ScanVehicleController {

    private Logger logger = LoggerFactory.getLogger(ScanVehicleController.class);
    @Autowired
    private IBinsVehicleInfoService vehicleService;

    // 获取车辆信息
    @GetMapping("/qrCode")
    public AjaxResult getQrcodeVehicleInfo(@RequestParam(value = "qrCode") String qrCode) {
        logger.info("qrCode: " + qrCode);
        BinsVehicleInfo vehicle = new BinsVehicleInfo();
        if (StringUtils.isNotBlank(qrCode)) {
            vehicle = vehicleService.getByQrCode(qrCode);
            return AjaxResult.success(vehicle);
        }
        return AjaxResult.success(vehicle);
    }

    // 获取车辆信息
    @GetMapping("/info")
    public AjaxResult getVehicleInfo( @RequestParam(value="vehicleCode") Long vehicleCode) {
        logger.info("vehicleCode: " + vehicleCode);
        BinsVehicleInfo vehicle = new BinsVehicleInfo();
        if (StringUtils.isNotNull(vehicleCode)) {
            vehicle = vehicleService.selectBinsVehicleInfoByVehicleCode(vehicleCode);
            return AjaxResult.success(vehicle);
        }
        return AjaxResult.success(vehicle);
    }

    // 执行开锁
    @PostMapping("/unlock")
    public AjaxResult unlockVehicle(@RequestBody Map dto) {
        logger.info("dto: " + dto);
        boolean isSucc = vehicleService.userUnlock(dto);
        return isSucc? AjaxResult.success():AjaxResult.error("车辆解锁异常");
    }

    @Autowired
    private IBinsMaintenanceInfoService maintenanceService;

    @PostMapping("/submit")
    public AjaxResult submitWarranty(@RequestBody WarrantyRecode warrantyRecode) {

        logger.info("warrantyRecode:{} " ,warrantyRecode);
        BinsMaintenanceInfo binsMaintenanceInfo = new BinsMaintenanceInfo();
        binsMaintenanceInfo.setVehicleCode(warrantyRecode.getVehicleId());
        if (null==(warrantyRecode.getVehicleId()) && StringUtils.isBlank(warrantyRecode.getLicenseNo())){
           return AjaxResult.error("车辆信息不可为空");
        }
        if(StringUtils.isNotBlank(warrantyRecode.getLicenseNo())){
            BinsVehicleInfo binsVehicleInfo = vehicleService.getByLicenseNo(warrantyRecode.getLicenseNo());
            if (null!=binsVehicleInfo){
                binsMaintenanceInfo.setVehicleCode(binsVehicleInfo.getVehicleCode());
            }else{
                return AjaxResult.error("车辆信息查询不到");
            }
        }
        binsMaintenanceInfo.setLicenseNo(warrantyRecode.getLicenseNo());
        binsMaintenanceInfo.setDeptId(warrantyRecode.getDeptId());
        binsMaintenanceInfo.setStatus(1);
        List<String> list = warrantyRecode.getFaultTypes();
        String description = StringUtils.defaultIfBlank(warrantyRecode.getDescription(), "");
        String faultValue = "详情：";
        if (list!=null && list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                String faultType = list.get(i);
                switch (faultType) {
                    case "1":
                        faultValue = faultValue+"1.电池故障";
                        break;
                    case "2":
                        faultValue = faultValue+"2.刹车失灵";
                        break;
                    case "3":
                        faultValue = faultValue+"3.车锁异常";
                        break;
                    case "4":
                        faultValue = faultValue+"4.GPS故障";
                        break;
                    case "5":
                        faultValue = faultValue+"5.其他故障";
                        break;
                    default:
                        faultValue = faultValue+"";
                        break;
                }
            }
        }else{
            faultValue = faultValue+"未汇报";
        }
        binsMaintenanceInfo.setRemark(description+faultValue);
        maintenanceService.insertBinsMaintenanceInfo(binsMaintenanceInfo);
        return AjaxResult.success();
    }

    @PostMapping("/return")
    public AjaxResult returnVehicle(@RequestBody Map dto) {
//        // 1. 验证车辆状态
//        VehicleStatus status = vehicleService.getStatus(dto.getVehicleId());
//        if (status != VehicleStatus.IN_USE) {
//            throw new ServiceException("该车辆未处于使用状态");
//        }
//
//        // 2. 创建保修记录
//        warrantyService.createRecord(dto);
//
//        // 3. 触发运维通知
//        maintenanceService.notify(dto.getVehicleId());

        return AjaxResult.success();
    }

}
